<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.dictKey" placeholder="字典Key" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.dictValue" placeholder="字典值" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <div style="float:right;">
        <el-button v-if="checkPermission(['sys:dict:add'])" class="filter-item" style="margin-left: 10px;" icon="el-icon-edit" @click="handleCreate">新增</el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      </div>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
      style="width: 100%;
            margin-left: 1px;
            border-top: 1px solid #dfe6ec;
            border-left: 1px solid #dfe6ec;
            border-right: 1px solid #dfe6ec;">
      <el-table-column label="字典Key" align="left">
        <template slot-scope="scope">
          <a @click="handleClick(scope.row)">{{ scope.row.dictKey }}</a>
        </template>
      </el-table-column>
      <el-table-column label="字典值" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.dictValue }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.describes }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拓展参数0" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.ext }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拓展参数1" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.ext1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拓展参数2" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.ext2 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="left">
        <template slot-scope="scope">
          <span v-if="scope.row.editTime">{{ scope.row.editTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['sys:dict:view','sys:dict:edit','sys:dict:del','sys:dict:item:list'])" label="搜索" align="center" width="160" class-name="small-padding fixed-width">
      <template slot-scope="scope">
        <el-tooltip v-if="checkPermission(['sys:dict:view'])" class="item action-icon" effect="dark" content="查看详情" placement="top">
          <a><svg-icon icon-class="ljm-view" class="action_icon icon-size" @click="handleView(scope.row)" /></a>
        </el-tooltip>
        <el-tooltip v-if="checkPermission(['sys:dict:edit'])" class="item action-icon" effect="dark" content="编辑" placement="top">
          <a><svg-icon icon-class="ljm-edit" class="action_icon icon-size" @click="handleUpdate(scope.row)" /></a>
        </el-tooltip>
        <el-tooltip v-if="checkPermission(['sys:dict:del'])" class="item action-icon" effect="dark" content="删除" placement="top">
          <a><svg-icon icon-class="ljm-del" class="action_icon icon-size" @click="handleDelete(scope.row)" /></a>
        </el-tooltip>
        <el-tooltip v-if="checkPermission(['sys:dict:item:list'])" class="item action-icon" effect="dark" content="条目" placement="top">
          <a><svg-icon icon-class="ljm-item" class="action_icon icon-size" @click="handleClick(scope.row)" /></a>
        </el-tooltip>
      </template>
    </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  import {delData, getList} from '@/api/sys/dict/index'

  export default {
  name: 'dict',
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        dictKey: '',
        dictValue: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      this.listLoading = false
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({ name: 'dict_mgr_add' })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        delData(row.id).then(() => {
          this.getList()
          this.listLoading = false
        }).catch(
          this.listLoading = false
        )
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleUpdate(row) {
      this.$router.push({ name: 'dict_mgr_eidt', query: { id: row.id }})
    },
    handleView(row) {
      this.$router.push({ name: 'dict_mgr_view', query: { id: row.id, view: 'view' }})
    },
    handleClick(row) {
      this.$router.push({ name: 'dictitem_mgr_list', query: { dictKey: row.dictKey }})
    }
  }
}
</script>
<style scoped>
  .action-icon{
    padding: 0 5px;
  }
  .icon-size{
    width: 17px;
    height: 17px;
  }
</style>
